 package com.fh.util.mail;

 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;

 /** 
  * UtilsTest 
  * 鏃堕棿鏃ユ湡宸ュ叿绫�,灏佽宸ヤ綔涓父鐢ㄧ殑涓�浜涙椂闂存棩鏈熻绠楁柟娉曠瓑 
  * 杩樺彲浠ユ彁渚涙洿澶氱殑閲嶈浇鏂规硶,鐢ㄤ簬鏃堕棿鐨勮浆鍖栫瓑 
  */  
 public class DateUtil {  
     private DateUtil(){  
           
     }  
       
     public static final String hhmmFormat="HH:mm";  
     public static final String MMddFormat="MM-dd";  
     public static final String yyyyFormat="yyyy";  
     public static final String yyyyChineseFormat="yyyy骞�";  
     public static final String yyyyMMddFormat="yyyy-MM-dd";
     public static final String fullFormat="yyyy-MM-dd HH:mm:ss";  
     public static final String MMddChineseFormat="MM鏈坉d鏃�";  
     public static final String yyyyMMddChineseFormat="yyyy骞碝M鏈坉d鏃�";  
     public static final String fullChineseFormat="yyyy骞碝M鏈坉d鏃� HH鏃秏m鍒唖s绉�";  
     public static final String [] WEEKS={"鏄熸湡鏃�","鏄熸湡涓�","鏄熸湡浜�","鏄熸湡涓�","鏄熸湡鍥�","鏄熸湡浜�","鏄熸湡鍏�"};  
     public static final String [] WEEKS_EN={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
     public static final String YMD_FORMAT = "yyyyMMdd";

     /** 
      * 寰楀埌鎸囧畾鏃堕棿鐨勬椂闂存棩鏈熸牸寮� 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @param format 鏃堕棿鏃ユ湡鏍煎紡 
      * @return 
 */  
     public static String getFormatDateTime(Date date,String format){
         DateFormat df=new SimpleDateFormat(format);  
         return df.format(date);  
     }  
     /** 
      * 寰楀埌鎸囧畾鏃堕棿鐨勬椂闂存棩鏈熸牸寮� 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @param format 鏃堕棿鏃ユ湡鏍煎紡 
      * @param land 璇█绫诲瀷 en=鑻辨枃锛寊h_CN=涓枃
      * @return 
      */  
     
     public static String getFormatDateTime(Date date,String format,String land){  
    	 String time; 
    	 String week;
         DateFormat df=new SimpleDateFormat(format); 
         time = df.format(date);
    	 week = land.equals("zh_CN") ? WEEKS[getWeek(date)-1] : WEEKS_EN[getWeek(date)-1];
         time = time +"  "+week;
         return time;  
     }

     public static String formatDate(Date date, String format){
         DateFormat df=new SimpleDateFormat(format);
         return df.format(date);
     }

     public static String formatDateYmd(Date date){
         DateFormat df=new SimpleDateFormat("yyyyMMdd");
         return df.format(date);
     }

     public static String formatDateYmdHms(Date date){
         DateFormat df=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
         return df.format(date);
     }

     public static Date stringToDateYmd(String dateString) {
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
         Date date = null;
         try {
             date = simpleDateFormat.parse(dateString);
         } catch (ParseException e) {
             e.printStackTrace();
         }
         simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
         String formattedTime = simpleDateFormat.format(date);

         date = java.sql.Date.valueOf(formattedTime);
         return date;
     }

     public static Date stringToDateYmdHms(String dateString) {
         Date date = null;
         try
         {
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             date = sdf.parse(dateString);
         }
         catch (ParseException e)
         {
             System.out.println(e.getMessage());
         }
         return date;
     }

     public static Date  parseDateYmdHms(String dateString) {
         Date date = null;
         try
         {
             SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
             date = sdf.parse(dateString);
         }
         catch (ParseException e)
         {
             throw new RuntimeException(e);
         }
         return date;
     }

     /** 
      * 鍒ゆ柇鏄惁鏄鼎骞� 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @return true:鏄鼎骞�,false:涓嶆槸娑﹀勾 
 */  
     public static boolean isLeapYear(Date date) {  
        Calendar cal=Calendar.getInstance();  
        cal.setTime(date);  
        return isLeapYear(cal.get(Calendar.YEAR));  
     }  
       
     /** 
      * 鍒ゆ柇鏄惁鏄鼎骞� 
      * @param year 鎸囧畾鐨勫勾
      * @return true:鏄鼎骞�,false:涓嶆槸娑﹀勾 
      */
     public static boolean isLeapYear(int year) {  
        GregorianCalendar calendar = new GregorianCalendar();  
        return calendar.isLeapYear(year);  
     }  
       
     /** 
      * 鍒ゆ柇鎸囧畾鐨勬椂闂存槸鍚︽槸浠婂ぉ 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @return true:鏄粖澶�,false:闈炰粖澶� 
 */  
     public static boolean isInToday(Date date){  
         boolean flag=false;  
         Date now=new Date();  
         String fullFormat=getFormatDateTime(now, DateUtil.yyyyMMddFormat);
         String beginString=fullFormat+" 00:00:00";  
         String endString=fullFormat+" 23:59:59";  
         DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
         try {  
             Date beginTime=df.parse(beginString);  
             Date endTime=df.parse(endString);  
             flag=date.before(endTime)&&date.after(beginTime);  
         } catch (ParseException e) {  
             e.printStackTrace();  
         }  
         return flag;  
     }  
       
     /** 
      * 鍒ゆ柇涓ゆ椂闂存槸鍚︽槸鍚屼竴澶� 
      * @param from 绗竴涓椂闂寸偣 
      * @param to 绗簩涓椂闂寸偣 
      * @return true:鏄悓涓�澶�,false:闈炲悓涓�澶� 
 */  
     public static boolean isSameDay(Date from,Date to){  
         boolean isSameDay=false;  
         DateFormat df=new SimpleDateFormat(DateUtil.yyyyMMddFormat);
         String firstDate=df.format(from);  
         String secondDate=df.format(to);  
         isSameDay=firstDate.equals(secondDate);  
         return isSameDay;  
     }  
       
     /** 
      * 姹傚嚭鎸囧畾鐨勬椂闂撮偅澶╂槸鏄熸湡鍑� 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @return 鏄熸湡X 
 */  
     public static String getWeekString(Date date){  
         return DateUtil.WEEKS[getWeek(date)-1];
     }  
       
     /** 
      * 姹傚嚭鎸囧畾鏃堕棿閭ｅぉ鏄槦鏈熷嚑 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @return 1-7 
 */  
     public static int getWeek(Date date){  
         int week=0;  
         Calendar cal=Calendar.getInstance();  
         cal.setTime(date);  
         week=cal.get(Calendar.DAY_OF_WEEK);  
         return week;  
     }  
   
       
     /** 
      * 鍙栧緱鎸囧畾鏃堕棿绂荤幇鍦ㄦ槸澶氬皯鏃堕棿浠ュ墠锛屽锛�3绉掑墠,2灏忔椂鍓嶇瓑 
      * 娉ㄦ剰锛氭璁＄畻鏂规硶涓嶆槸绮剧‘鐨� 
      * @param date 宸叉湁鐨勬寚瀹氭椂闂� 
      * @return 鏃堕棿娈垫弿杩� 
 */  
     public static String getAgoTimeString(Date date){  
         Date now=new Date();  
         Calendar cal=Calendar.getInstance();  
         cal.setTime(date);  
         Date agoTime=cal.getTime();  
         long mtime=now.getTime()-agoTime.getTime();  
         String str="";  
         long stime=mtime/1000;  
         long minute=60;  
         long hour=60*60;  
         long day=24*60*60;  
         long weeks=7*24*60*60;  
         long months=100*24*60*60;  
         if(stime<minute){  
             long time_value=stime;  
             if(time_value<=0){  
                 time_value=1;  
             }  
             str=time_value+"绉掑墠";  
         }else if(stime>=minute && stime<hour){  
             long time_value=stime/minute;  
             if(time_value<=0){  
                 time_value=1;  
             }  
             str=time_value+"鍒嗗墠";  
         }else if(stime>=hour && stime<day){  
             long time_value=stime/hour;  
             if(time_value<=0){  
                 time_value=1;  
             }  
             str=time_value+"灏忔椂鍓�";  
         }else if(stime>=day&&stime<weeks){  
             long time_value=stime/day;  
             if(time_value<=0){  
                 time_value=1;  
             }  
             str=time_value+"澶╁墠";  
         }else if(stime>=weeks&&stime<months){  
             DateFormat df=new SimpleDateFormat(DateUtil.MMddFormat);
             str=df.format(date);  
         }else{  
             DateFormat df=new SimpleDateFormat(DateUtil.yyyyMMddFormat);
             str=df.format(date);  
         }  
         return str;  
     }  
       
     /** 
      * 鍒ゆ柇鎸囧畾鏃堕棿鏄惁鏄懆鏈� 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @return true:鏄懆鏈�,false:闈炲懆鏈� 
 */  
     public static boolean isWeeks(Date date){  
         boolean isWeek=false;  
         isWeek=(getWeek(date)-1==0||getWeek(date)-1==6);  
         return isWeek;  
     }  

     /**
      * 鑾峰彇褰撳墠鏃ユ湡
      * @return Date
      * */
     public static Date getToday(){
         String date = DateUtil.formatDate(new Date(), YMD_FORMAT);
         return DateUtil.stringToDateYmd(date);
     }

     /**
      * 鑾峰彇褰撳墠鏃ユ湡
      * @return Date
      * */
     public static String getTodayStrYmd(){
         String date = DateUtil.formatDate(new Date(), YMD_FORMAT);
         return date;
     }

     /** 
      * 寰楀埌浠婂ぉ鐨勬渶寮�濮嬫椂闂� 
      * @return 浠婂ぉ鐨勬渶寮�濮嬫椂闂� 
 */  
     public static Date getTodayBeginTime(){  
         String beginString= DateUtil.yyyyMMddFormat+" 00:00:00";
         DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
         Date beginTime=new Date();  
         try {  
             beginTime=df.parse(beginString);  
         } catch (ParseException e) {  
             e.printStackTrace();  
         }  
         return beginTime;  
     }  
       
     /** 
      * 寰楀埌浠婂ぉ鐨勬渶鍚庣粨鏉熸椂闂� 
      * @return 浠婂ぉ鐨勬渶鍚庢椂闂� 
 */  
     public static Date getTodayEndTime(){  
         String endString= DateUtil.yyyyMMddFormat+" 23:59:59";
         DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
         Date endTime=new Date();  
         try {  
             endTime=df.parse(endString);  
         } catch (ParseException e) {  
             e.printStackTrace();  
         }  
         return endTime;  
     }  
       
     /** 
      * 鍙栧緱鏈懆鐨勫紑濮嬫椂闂� 
      * @return 鏈懆鐨勫紑濮嬫椂闂� 
 */  
     public static Date getThisWeekBeginTime(){  
         Date beginTime=null;  
         Calendar cal=Calendar.getInstance();  
         int week=getWeek(cal.getTime());  
         week=week-1;  
         int days=0;  
         if(week==0){  
             days=6;  
         }else{  
             days=week-1;  
         }  
         cal.add(Calendar.DAY_OF_MONTH, -days);  
         beginTime=cal.getTime();  
         return beginTime;  
     }  
       
     /** 
      * 鍙栧緱鏈懆鐨勫紑濮嬫棩鏈� 
      * @param format 鏃堕棿鐨勬牸寮� 
      * @return 鎸囧畾鏍煎紡鐨勬湰鍛ㄦ渶寮�濮嬫椂闂� 
 */  
     public static String getThisWeekBeginTimeString(String format){  
         DateFormat df=new SimpleDateFormat(format);  
         return df.format(getThisWeekBeginTime());  
     }  
       
       
     /** 
      * 鍙栧緱鏈懆鐨勭粨鏉熸椂闂� 
      * @return 鏈懆鐨勭粨鏉熸椂闂� 
 */  
     public static Date getThisWeekEndTime(){  
         Date endTime=null;  
         Calendar cal=Calendar.getInstance();  
         int week=getWeek(cal.getTime());  
         week=week-1;  
         int days=0;  
         if(week!=0){  
             days=7-week;  
         }  
         cal.add(Calendar.DAY_OF_MONTH, days);  
         endTime=cal.getTime();  
         return endTime;  
     }  
       
       
     /** 
      * 鍙栧緱鏈懆鐨勭粨鏉熸棩鏈� 
      * @param format 鏃堕棿鐨勬牸寮� 
      * @return 鎸囧畾鏍煎紡鐨勬湰鍛ㄧ粨鏉熸椂闂� 
 */  
     public static String getThisWeekEndTimeString(String format){  
         DateFormat df=new SimpleDateFormat(format);  
         return df.format(getThisWeekEndTime());  
     }  
       
     /** 
      * 鍙栧緱涓ゆ椂闂寸浉宸殑澶╂暟 
      * @param from 绗竴涓椂闂� 
      * @param to 绗簩涓椂闂� 
      * @return 鐩稿樊鐨勫ぉ鏁� 
 */  
     public static long getBetweenDays(Date from, Date to){  
         long days=0;  
         long dayTime=24*60*60*1000;  
         long fromTime=from.getTime();  
         long toTime=to.getTime();  
         long times=Math.abs(fromTime-toTime);  
         days=times/dayTime;  
         return days;  
     }  
       
     /** 
      * 鍙栧緱涓ゆ椂闂寸浉宸殑灏忔椂鏁� 
      * @param from 绗竴涓椂闂� 
      * @param to 绗簩涓椂闂� 
      * @return 鐩稿樊鐨勫皬鏃舵暟 
 */  
     public static long getBetweenHours(Date from,Date to){  
         long hours=0;  
         long hourTime=60*60*1000;  
         long fromTime=from.getTime();  
         long toTime=to.getTime();  
         long times=Math.abs(fromTime-toTime);  
         hours=times/hourTime;  
         return hours;  
     }  
       
     /** 
      * 鍙栧緱鍦ㄦ寚瀹氭椂闂翠笂鍔犲噺days澶╁悗鐨勬椂闂� 
      * @param date 鎸囧畾鐨勬椂闂� 
      * @param days 澶╂暟,姝ｄ负鍔狅紝璐熶负鍑� 
      * @return 鍦ㄦ寚瀹氭椂闂翠笂鍔犲噺days澶╁悗鐨勬椂闂� 
 */  
     public static Date addDays(Date date,int days){  
         Date time=null;  
         Calendar cal=Calendar.getInstance();  
         cal.add(Calendar.DAY_OF_MONTH, days);  
         time=cal.getTime();  
         return time;  
     }  
       
     /** 
      * 鍙栧緱鍦ㄦ寚瀹氭椂闂翠笂鍔犲噺months鏈堝悗鐨勬椂闂� 
      * @param date 鎸囧畾鏃堕棿 
      * @param months 鏈堟暟锛屾涓哄姞锛岃礋涓哄噺 
      * @return 鍦ㄦ寚瀹氭椂闂翠笂鍔犲噺months鏈堝悗鐨勬椂闂� 
 */  
     public static Date addMonths(Date date,int months){  
         Date time=null;  
         Calendar cal=Calendar.getInstance();  
         cal.add(Calendar.MONTH, months);  
         time=cal.getTime();  
         return time;  
     }  
       
     /** 
      * 鍙栧緱鍦ㄦ寚瀹氭椂闂翠笂鍔犲噺years骞村悗鐨勬椂闂� 
      * @param date 鎸囧畾鏃堕棿 
      * @param years 骞存暟锛屾涓哄姞锛岃礋涓哄噺 
      * @return 鍦ㄦ寚瀹氭椂闂翠笂鍔犲噺years骞村悗鐨勬椂闂� 
 */  
     public static Date addYears(Date date,int years){  
         Date time=null;  
         Calendar cal=Calendar.getInstance();  
         cal.add(Calendar.YEAR, years);  
         time=cal.getTime();  
         return time;  
     }

     /**
      * @param args 
 */  
     public static void main(String[] args) {  
    	 
    	 System.out.println(getFormatDateTime(new Date(), DateUtil.yyyyMMddFormat,"en"));
    	 
        /* System.out.println(getFormatDateTime(new Date(),DateUtil.fullChineseFormat));  
         System.out.println(isLeapYear(new Date()));  
         Calendar cal=Calendar.getInstance();  
         System.out.println(isInToday(cal.getTime()));  
         Calendar cal2=Calendar.getInstance();  
         cal2.set(2011, 06, 05);  
         System.out.println(isSameDay(cal.getTime(),cal2.getTime()));  
         System.out.println(WEEKS[getWeek(new Date())]);  //鏄熸湡鍑�
         DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);  
         String fullString="2011-06-03 22:37:20";  
         try {  
             Date fulldate=df.parse(fullString);  
             System.out.println(getBetweenDays(fulldate,cal.getTime()));  
             System.out.println("ago:"+getAgoTimeString(fulldate));  
             System.out.println(isWeeks(fulldate));  
         } catch (ParseException e) {  
             e.printStackTrace();  
         }  
         
         System.out.println(getThisWeekBeginTimeString(DateUtil.yyyyMMddChineseFormat));  
         System.out.println(getThisWeekEndTimeString(DateUtil.yyyyMMddChineseFormat));  
         System.out.println(addDays(new Date(),3));  
         System.out.println(addDays(new Date(),-3));  
         System.out.println(addMonths(new Date(),2));  
         System.out.println(addMonths(new Date(),-2));  
         System.out.println(addYears(new Date(),1));  
         System.out.println(addYears(new Date(),-1));  
           */
     }  
 }